package cn.vortex.device.dao.impl;

import cn.vortex.device.tool.TxQueryRunner;
import cn.vortex.device.dao.UserDao;
import cn.vortex.device.vo.Device;
import cn.vortex.device.vo.User;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl extends AbstractBaseDaoImpl<User> implements UserDao {

    //Oracle
    private static final String BASE_FIELD = " id,account,password,username,roleID ";

    private QueryRunner queryRunner = new TxQueryRunner();

    @Override
    public List<User> findAll() throws Exception {

        String sql = "select" + BASE_FIELD + "from t_user";
        return findListObj(sql, Device.class,null);
    }

    @Override
    public int update(User user) throws SQLException {

        String sql = "update t_user set password = ?, username = ?, roleid = ? where id = ?";

        return queryRunner.update(sql, user.getPassword(), user.getUsername(),
                user.getRoleId(), user.getId());
    }

    @Override
    public int insert(User user) throws SQLException {

        String sql = "insert into t_user(account, password, username, roleID) values(?,?,?,?)";

        return queryRunner.update(sql, user.getAccount(), user.getPassword(), user.getUsername(),
                user.getRoleId());
    }

    @Override
    public int deleteById(Integer id) throws SQLException {
        String sql = "delete from t_user where id = ?";
        return queryRunner.update(sql,id);
    }

    @Override
    public User findByAccount(String account) throws Exception {
        String sql = "select" + BASE_FIELD + "from t_user where account = ?";
        return findObject(sql, User.class, account);
    }

    @Override
    public User findById(Integer id) throws Exception {
        String sql = "select" + BASE_FIELD + "from t_user where id = ?";
        return findObject(sql, User.class, id);
    }


}
